package com.sad.we11cis;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ReviewRegister extends Activity implements OnClickListener {

	String revTitle = null;
	String revContent = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_review_register);
		
		Button regiBtn = (Button)findViewById(R.id.regiRevBtn);
		regiBtn.setOnClickListener(this);
		Button srcBtn = (Button)findViewById(R.id.btnSrCategory);
		srcBtn.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.review_register, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		EditText title = (EditText)findViewById(R.id.revTitle);
		EditText contents = (EditText)findViewById(R.id.revContents);
		
		switch(v.getId()){
		
			case R.id.regiRevBtn:
				
				revTitle = title.getText().toString();
				revContent = contents.getText().toString();
				
				sendReview send = new sendReview();
				send.execute();
				
				break;
				
			case R.id.btnSrCategory:
				Intent categoryPage = new Intent(ReviewRegister.this, SelectCategory.class);
				startActivity(categoryPage);
				break;
		}
	}
	
	private class sendReview extends AsyncTask<Void, Void, Void>{

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			
			try{
                URL url = new URL("http://192.168.101.10:8080/Weening11evenServer/CommunicateServlet");
                URLConnection connection = url.openConnection();

                Log.d("Title1", revTitle);

                connection.setDoOutput(true);
                
				OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
				out.write(revTitle + "/" + revContent);
				out.close();

				//receive result of login from server
				String userRank = "";
				BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				userRank = in.readLine();
				in.close();

                //BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                }catch(Exception e)
                {
                    Log.d("Exception",e.toString());
                }
			
			return null;
		}
		
	}

}
